global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"

capture log close
log using "$log/3_balanced_firms_wr", replace

*Keep only the firm names for each month
cap program drop loop_clean
program define loop_clean 
args year month 
	clear all
	
	*Clean each month to only keep employer name
	forvalues i=0/60 {
	    local m=mod(`month'-1+`i',12)+1
	    local y=`year'+int((`month'-1+`i')/12)
		capture use yr_month client_id using "$temp\cleaned_`y'`m'", clear 
		capture use yr_month client_id using "$temp\cleaned_`y'0`m'", clear
		
		*Keep only a list of firms 
		duplicates drop client_id, force
		
		compress
		save "$temp\temp_`y'`m'", replace
	}
end
loop_clean 2013 5

*Balanced firms that exist every month for 3 years around May 2016
cap program drop balanced
program define balanced 
args year month ref panel
	*Append all months
	clear
	forvalues i=0/36 {
	    local m=mod(`month'-1+`i',12)+1
	    local y=`year'+int((`month'-1+`i')/12)
		append using "$temp\temp_`y'`m'", keep(yr_month client_id)  
	}
	
	*Keep firms that show up in every month
		gen n=1
		collapse (count) months_open=n, by(client_id)	
		tab months_open
		keep if months_open==`panel'
		keep client_id
		save "$output/balanced_firms_`ref'", replace
end
balanced 2015 5 treat 37 
balanced 2013 5 control 37


*Delete temp files
forvalues year=2013/2018 {
    forvalues month=1/12{
	   capture erase "$temp\temp_`year'`month'.dta"
	}
}

log close